package ooday04;

public class MultiTypeDemo {
    public static void main(String[] args) {
        /*
        //强转
        Animal o = new Dog("小黑", 2, "黑色");
        Dog g = (Dog) o;//引用o所指向的对象，就是Dog类型
        Swim s = (Swim) o;//引用o所指向的对象，实现了Swim接口
        //Fish f = (Fish) o;//运行时会发生ClassCastException类型转换异常

        System.out.println(o instanceof Dog);  //true
        System.out.println(o instanceof Swim); //true
        System.out.println(o instanceof Fish); //false

        if (o instanceof Fish) {
            Fish f = (Fish) o;
        } else {
            System.out.println("o不是Fish类型");
        }

         */

        Animal[] animals = new Animal[5];
        animals[0] = new Dog("小黑", 2, "黑色");
        animals[1] = new Dog("小白", 2, "白色");
        animals[2] = new Chick("小红", 2, "红色");
        animals[3] = new Chick("小花", 2, "花色");
        animals[4] = new Fish("小金", 2, "金色");
        for (int i = 0; i < animals.length; i++) {
            System.out.println(animals[i].name);
            animals[i].eat();
            animals[i].drink();

            if (animals[i] instanceof Dog) {
                Dog dog = (Dog) animals[i];
                dog.lookHome();
            }
            if (animals[i] instanceof Chick) {
                Chick chick = (Chick) animals[i];
                chick.layEggs();
            }
            if (animals[i] instanceof Swim) {//适用于所有实现Swim接口的
                Swim s = (Swim) animals[i];
                s.swim();
            }
        }


        /*
        Master master = new Master();
        Dog dog = new Dog("小黑", 2, "黑色");
        Chick chick = new Chick("小花", 2, "花色");
        Fish fish = new Fish("小红", 2, "红色");
        master.feed(dog);
        master.feed(chick);
        master.feed(fish);

         */
        /*
        //向上造型
        Dog o1 = new Dog("小黑", 2, "黑色");
        //o1.name/age/color/eat()/drink()/swim()/lookHome()
        Animal o2 = new Dog("小黑", 2, "黑色");
        //o2.name/age/color/eat()/drink()
        Swim o3 = new Dog("小黑", 2, "黑色");
        //o3.swim

         */
    }
}
